import http from 'http'
// import net from 'net'
import { URL } from 'url'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function request(cIncomeReq: any, cRes: any) {
  console.log('AT-[ cIncomeReq &&&&&********** ]', cIncomeReq)
  const u = new URL(cIncomeReq.url)

  const options = {
    hostname: u.hostname,
    port: u.port || 80,
    host: u.host,
    method: cIncomeReq.method,
    headers: cIncomeReq.headers,
  }

  const pReq = http
    .request(options, function (pIncomeRes) {
      cRes.writeHead(pIncomeRes.statusCode, pIncomeRes.headers)
      pIncomeRes.pipe(cRes)
    })
    .on('error', function () {
      cRes.end()
    })

  cIncomeReq.pipe(pReq)
}

http
  .createServer()
  .on('request', request)
  .listen(8888, '0.0.0.0', () => {
    console.log('http代理已启动')
  })
